﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace ROrganizer.Client.Crawling
{
    internal class MessageSerializer
    {
        public MessageEntry Deserialize(XDocument document, string source)
        {
            var query = from element in document.Root.Elements()
                        where element.Element("Source").Value == source
                        select MessageSerializer.Deserialize(element);

            return query.FirstOrDefault();
        }

        public MessageEntry[] Deserialize(XDocument document)
        {
            var query = from element in document.Root.Elements()
                        select MessageSerializer.Deserialize(element);

            return query.ToArray();
        }

        private static MessageEntry Deserialize(XElement element)
        {
            return new MessageEntry
            {
                Id = Guid.Parse(element.Attribute("id").Value),
                Title = element.Element("Title").Value,
                Link = element.Element("Link").Value,
                Source = element.Element("Source").Value,
                Size = Int64.Parse(element.Element("Size").Value),
                Imdb = element.Element("Imdb").Value,
                Hash = element.Element("Hash").Value,
                Movie = element.Element("Movie").Value,
                Location = element.Element("Location").Value,
                Published = element.Element("Published") != null && Boolean.Parse(element.Element("Published").Value)
            };
        }

        public void Update(XDocument document, params MessageEntry[] entries)
        {
            foreach (var entry in entries)
            {
                var element = document.Root.Elements().FirstOrDefault(x => x.Element("Source").Value == entry.Source);
                var created = MessageSerializer.Serialize(entry);

                if (element != null)
                {
                    element.ReplaceWith(created);
                }
                else
                {
                    document.Root.Add(created);
                }
            }
        }

        private static XElement Serialize(MessageEntry entry)
        {
            return new XElement("CrawlerEntry",
                        new XAttribute("id", entry.Id),
                        new XElement("Title", new XCData(entry.Title)),
                        new XElement("Link", new XCData(entry.Link)),
                        new XElement("Source", new XCData(entry.Source)),
                        new XElement("Size", entry.Size),
                        new XElement("Imdb", entry.Imdb),
                        new XElement("Hash", entry.Hash),
                        new XElement("Movie", new XCData(entry.Movie)),
                        new XElement("Location", new XCData(entry.Location)),
                        new XElement("Published", entry.Published));
        }
    }
}
